package com.yhh;

import com.yhh.Entity.Command;
import com.yhh.Entity.HandlerCommand;
import com.yhh.Exceptions.InstructionException;
import com.yhh.Exceptions.NonInstructionException;

import java.io.*;
import java.net.Socket;
import java.util.Map;

import static com.yhh.myUtils.CommandUtils.hasFile;

class SendCommand extends Command implements HandlerCommand {
    private FileInputStream fis;
    private DataOutputStream dos;

    protected SendCommand(Socket socket) {
        super(socket);
        command.add("send");
        command.add("-d");
        command.add("-t");
        command.add("-f");
    }

    @Override
    public void handlerParameter(Map<String, String> args) throws FileNotFoundException {
        String path = args.get("-f");
        hasFile(path);
    }

    @Override
    public boolean check(Map<String, String> args) throws  InstructionException {
        if (args.get("-f") == null) throw new InstructionException("需要一个-f参数", "send");
        try {
            handlerParameter(args);
        } catch (FileNotFoundException e) {
            System.out.println("对应文件不存在");
            return false;
        }
        checkParameter(args);

        return true;
    }

    @Override
    public void handlerCommand(Map<String, String> args) throws InstructionException, NonInstructionException {
        String resp = recvMsg();
        if("ready".equals(resp)){
            handlerSend(args.get("-f"));
        }else{
            System.out.println(resp);
            return;
        }
        System.out.println(recvMsg());
    }

    public void handlerSend(String filename) {
        try {

            File file = hasFile(filename);
            fis = new FileInputStream(file);
            dos = new DataOutputStream(socket.getOutputStream());

            // 文件名和长度
            dos.writeUTF(file.getName());
            dos.flush();
            dos.writeLong(file.length());
            dos.flush();


            // 开始传输文件
            System.out.println("======== 开始传输文件 ========");
            byte[] bytes = new byte[1024];
            long l = file.length() / 1024 / 100;
            int length = 0;
            long progress = 0;
            long i = l;
            while ((length = fis.read(bytes, 0, bytes.length)) != -1) {
                dos.write(bytes, 0, length);
                dos.flush();
                progress += length;
                if (i-- == 0) {
                    System.out.print("| " + (100 * progress / file.length()) + "% |");
                    i = l;
                }
            }
            System.out.println();
            System.out.println("======== 文件传输成功 ========");
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在,重新输入");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
